# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import random
from dra100_scrapy.mysql import DB
from dra100_scrapy.qiniu import qiniuServer
from scrapy.pipelines.images import ImagesPipeline
from scrapy.exceptions import DropItem
from scrapy.http import Request
from scrapy.utils.project import get_project_settings

class ScrapyPipeline(object):
    def process_item(self, item, spider):
        return item


class CustomImagesPipeline(ImagesPipeline):

    def get_media_requests(self, item, info):
        for image_url in item['image_urls']:
            yield Request(image_url)

    def item_completed(self, results, item, info):

        image_paths = []
        for ok, x in results:
            if (ok):
                image_paths.append(x) 

        settings = get_project_settings()
        qs = qiniuServer()
        i = 0
        for image in image_paths:
            file_name = image['path'].split('/')[-1]
            file_path = settings['IMAGES_STORE'] + image['path']
            img_url = settings['IMAGES_DOMAIN'] + file_name
            if(qs.upload(file_name, file_path)):
                item['content'] = item['content'].replace(image['url'], img_url);
                if i == 0: 
                    item['img_url'] = img_url
            i += 1

        if len(item['img_url']) == 0:
            item['img_url'] = 'http://img.healthmudi.com/default/default_' + str(random.randint(1, 95)) + '.jpg'

        db = DB();
        db.save_article(item)

        return item
